import React from "react";
import { Navigate } from "react-router-dom";
const Home = React.lazy(() => import("../pages/home"));
const Login = React.lazy(() => import("../pages/login"));
const PersonAdd = React.lazy(() => import("../pages/personAdd"));
const PersonDetail = React.lazy(() => import("../pages/personDetail"));
const PersonList = React.lazy(() => import("../pages/personList"));
const PersonVerify = React.lazy(() => import("../pages/personVerify"));
const Mainlayout = React.lazy(() => import("../mainLayout"));

const PrivateRouter = ({ children }) => {
  const token = localStorage.getItem("token");

  if (token) {
    return children;
  } else {
    return <Navigate to="/login"></Navigate>;
  }
};

export const routes = [
  {
    path: "/",
    element: (
      <PrivateRouter>
        <Mainlayout></Mainlayout>
      </PrivateRouter>
    ),
    children: [
      {
        path: "/person/add",
        element: <PersonAdd></PersonAdd>,
      },
      {
        path: "/person/list",
        element: <PersonList></PersonList>,
      },
      {
        path: "/person/verify",
        element: <PersonVerify></PersonVerify>,
      },
      {
        path: "/person/detail/:id",
        element: <PersonDetail></PersonDetail>,
      },
      {
        path: "/home",
        element: <Home></Home>,
      },
      {
        path: "/",
        element: <Navigate to="/home"></Navigate>,
      },
      {
        path: "*",
        element: <div>404 页面没找到~</div>,
      },
    ],
  },
  {
    path: "/login",
    element: <Login></Login>,
  },
  {
    path: "*",
    element: <div>404 页面没找到~</div>,
  },
];
